#include <fstream>
#include <iostream>
#include <string>
using namespace std ;

int main( int argc, char * argv[] )
{
	unsigned char pmagic[4] = {0x3a, 0x19, 0x5d, 0xf7};
	unsigned int magic_number = *((unsigned int *)pmagic);

	if( argc < 2 ) {
		cout << "Using encrypt <filename>" << endl ;
		return 1 ;
	}

	string filename ;
	filename = argv[1] ;

	ifstream ifs ;
	ifs.open( filename.c_str(), ios::binary | ios::in ) ;
	if ( !ifs )
	{
		cout << "Can't open file." << endl ;
		return 1 ;
	}

	filename += "e" ;
	ofstream ofs ;
	ofs.open( filename.c_str(), ios::binary | ios::out ) ;
	if ( !ofs )
	{
		cout << "Can't create file." << endl ;
		return 1 ;
	}

	const int blocksize = 512 ;
	char buffer[blocksize] ;
	ifs.read( buffer, blocksize ) ;
	if( ifs.gcount() < blocksize )
	{
		cout << "Too small." << endl ;
		return 1 ;
	}

	unsigned int * p ;
	p = (unsigned int *)buffer ;

	for( int i=0; i<0x80; i++ )
	{
		unsigned int ui ;
		ui = *p ;
		ui = ui ^ magic_number ;
		*p = ui ;
		p++ ;
	}

	ofs.write( buffer, blocksize ) ;

	while( ! ifs.eof() )
	{
		ifs.read( buffer, blocksize ) ;
		ofs.write( buffer, ifs.gcount() ) ;
	}
	ifs.close() ;
	ofs.close() ;

	return 0;
}

